<template>
	<view class="container">
<!--		<view class="take">-->
<!--			<view class="item" :class="{active:form.take==0}" @click="form.take=0">-->
<!--				快递-->
<!--			</view>-->
<!--			<view class="item" :class="{active:form.take==1}" @click="form.take=1">-->
<!--				自提-->
<!--			</view>-->
<!--		</view>-->
<view class="bor" style="height: 10rpx;background-color: #F6F6F6;">

</view>
		<view class="header box_cnt" @click="$goURl('/pages/personal/address/list?typeStr=1')">
			<view class="left" v-if="JSON.stringify(address)!='{}'">
				<view class="address">

					{{address.name}} {{address.mobile}}
				</view>
				<view class="addressxx">
					{{address.province}}{{address.city}}{{address.area}}{{address.detailed}}

				</view>
			</view>
			<view class="left" v-else>
				<view class="addressxx">
					请选择地址
				</view>
			</view>
			<uni-icons type="forward" size="20"></uni-icons>
		</view>
		<view class="box_cnt" v-for="(index,item) in goodslist">
			<view class="">
				<view class="goodsbox box_cnt">
				<!-- 	<view class="business">
						<image :src="index[0].business.shop_head" mode="" v-if="index[0].business.shop_head"></image>
						{{index[0].business.name?index[0].business.name:'自营商品'}}
					</view> -->
					<view class="goods" v-for="(items,indexs) in index">
						<image :src="items.goods.img" mode="aspectFill"></image>
						<view class="right">
							<view class="name textsl1">
								{{items.goods.name}}
							</view>
							<view class="num">
								<view>
									规格：{{items.attributes}}
								</view>
								<view v-if="items.coupon>0">{{items.is_single==4?'DL':'ZKFC'}}：{{items.coupon}}</view>
							</view>
							<view class="num" style="margin-top: 8px;">
								<view>
									商品分区：{{items.partition}}
								</view>

							</view>
							<view class="gg">
								<text style="margin-right: 10px;color: #976029;font-size: 32rpx;" v-show="items.price>0">￥{{items.price}}</text>
								<text v-if="items.coupon>0">积分：{{items.coupon}}</text>
								<view class="delOrder" style="top:150rpx;font-size: 28rpx;color: #2D2D2D;">X{{items.number}}</view>
							<!-- 	<uni-icons class="delOrder" type="trash" size="20" color="red" v-if="items.source==0"
									@click="delOrder(items.ID,item,indexs)"></uni-icons> -->
							</view>
							<view class="gg">
								<text style="margin-right: 10px;color: red;"
									v-if="items.is_deliver == 1">该地区该商品不发货</text>
							</view>
						</view>
					</view>
					<view class="titlebox">
						<view class="titleboxl">
							现金
						</view>
						<view class="titleboxr titlecolors">
							￥{{$toFiexd(Number(totalprice) ,2)}}
						</view>
					</view>
					<view class="titlebox" v-if="zkfc>0">
						<view class="titleboxl">
							{{goodslist[0]&&goodslist[0][0].is_single==4?'DL':'ZKFC'}}
						</view>
						<view class="titleboxr titlecolors">
							￥{{$toFiexd(Number(zkfc),2)}}
						</view>
					</view>
					<view class="titlebox" v-if="form.take==0">
						<view class="titleboxl">
							运费
						</view>
						<view class="titleboxr titlecolors">
							￥{{$toFiexd(index[0].freight,2)}}
						</view>
					</view>
					<view class="titlebox">
						<view class="titleboxl">
							总价
						</view>
						<view class="titleboxr titlecolors">
							￥{{$toFiexd(Number(totalprice) + Number(freight)+Number(zkfc),2)}}
						</view>
					</view>

					<view class="titlebox">
						<view class="titleboxl">
							配送
						</view>
						<view class="titleboxr">
							{{form.take==0?"快递":"自提"}}
						</view>
					</view>

				</view>

			</view>
		</view>
		<!-- <view class="goodsbox">
			<view class="titlebox">
				<view class="titleboxl">
					买家留言：
				</view>
				<input class="uni-input" v-model="form.remarks" name="input" placeholder="选填，填写内容请和卖家协商确认" />
			</view>
		</view> -->
		<view class="backList box_cnt" v-if="form.pay_type==4" @click="binding">
			<view class="backNames">
				<image class="backImg" src="https://pubuimg.cqguanjie.cn/chat/16950024915493"></image>
				<text class="backId">{{data.bakn.bank_card_no||'请选择银行卡'}}</text>
			</view>
			<image class="selects" src="https://pubuimg.cqguanjie.cn/chat/16950033913179"></image>
		</view>
		<view class="itembox box_cnt" v-if="data.bakn.mobile_no&&form.pay_type==4">
			<view class="label">
				开户手机号:
			</view>
			<view class="input" style="display: flex;align-items: center;font-size: 10px;">
				{{data.bakn.mobile_no}}
				<view class="time" @click="data.time=='获取验证码'?pay(0):''">
					{{data.time}}
				</view>
			</view>

		</view>
		<view class="itembox  box_cnt" v-if="data.bakn.mobile_no&&form.pay_type==4">
			<view class="label">
				验证码:
			</view>

			<uni-easyinput class="input" v-model="form.sms_code" placeholder="请输入验证码" />
		</view>
		<view class="goodsbox goodsbox2 box_cnt"  v-if="paylist.length!=0">
			<radio-group @change="radioChange">
				<label class="" v-for="(item, index) in paylist" :key="item.value">
					<view class="paylist" v-if="item.show=='1'">
						<view class="left">
							<image :src="item.url" mode="aspectFill"></image>{{item.name}}{{item.id==0?`(${userInfo.money})`:''}}{{item.id==5?`(${userInfo.coupon})`:''}}
						</view>
						<view>
							<radio color="#F63B29" :value="item.id" :checked="item.id == form.pay_type" />
						</view>
					</view>
				</label>
			</radio-group>
		</view>
		<view class="" style="height: 115px;">

		</view>
		<view class="footer">
		<!-- 	<view class="time">
				<uni-notice-bar scrollable single text="请在10分钟内完成支付，如未支付完成该订单将自动取消"></uni-notice-bar>
			</view> -->
			<view class="foot">
				<view class="left">
						实付款
					<!-- 快递 -->
					<view class="price" v-if="form.take==0">
						<!-- ￥{{$toFiexd(Number(totalprice) + Number(freight)+Number(zkfc),2)}} -->
						<view v-if="goodslist[0]&&goodslist[0][0].is_single==4">
							<text v-if="userInfo.dl>=zkfc">￥{{$toFiexd(Number(totalprice)+ Number(freight),2)}}</text>
							<text
								v-else>￥{{$toFiexd(Number(totalprice) + Number(freight)+Number(zkfc)-Number(userInfo.dl),2)}}</text>
						</view>
						<view v-else>
							<text
								v-if="userInfo.zkfc>=zkfc">￥{{$toFiexd(Number(totalprice)+ Number(freight) ,2)}}</text>
							<text
								v-else>￥{{$toFiexd(Number(totalprice) + Number(freight)+Number(zkfc)-Number(userInfo.zkfc||0),2)}}</text>
						</view>
					</view>
					<view class="price" v-if="form.take==1">
						<view v-if="goodslist[0]&&goodslist[0][0].is_single==4">
							<text v-if="userInfo.dl>=zkfc">￥{{$toFiexd(Number(totalprice),2)}}</text>
							<text v-else>￥{{$toFiexd(Number(totalprice) +Number(zkfc)-Number(userInfo.dl),2)}}</text>
						</view>
						<view v-else>
							<text v-if="userInfo.zkfc>=zkfc">￥{{$toFiexd(Number(totalprice) ,2)}}</text>
							<text v-else>￥{{$toFiexd(Number(totalprice) +Number(zkfc)-Number(userInfo.zkfc),2)}}</text>
						</view>
					</view>
					<!-- 	<view style="margin-left: 10rpx; font-size: 14px;" class="price" v-if="zkfc>0">
						+ <view style="font-size: 13px; margin: 0px 5px;"> ZKFC</view>   {{$toFiexd(zkfc,2)}}
					</view> -->
				</view>
				<view class="right" @click="pay(1)">
					立即购买
				</view>
			</view>
		</view>
		<!-- 银行卡列表 -->
		<uni-popup ref="share" class="sharep" style="border-top-left-radius: 20px;border-top-right-radius: 20px;">
			<view class="popupcontent">
				<view class="selectBank">
					<text class="selectMdd">选择银行卡</text>
					<uni-icons type="closeempty" size="20" class="popupIcon" @click="share.close()"></uni-icons>
				</view>
				<view class="information">
					<view class="BankList" v-for="(lin,index) in data.list" :key="index" @click="selectBank(lin)">
						<view class="nameAndBankName">
							<text class="name">{{lin.name}}</text>
						</view>
						<view class="bankAddress">
							<text>{{lin.bank_card_no}}</text>
						</view>
					</view>
				</view>
				<view class="btn" @click="$goURl('/pages/order/banklist')">
					添加银行卡
				</view>
			</view>
		</uni-popup>
		<view class="" style="width: 0;height: 0;" v-if="data.html">
			<web-view :src="data.html" style="width: 0;height: 0;"></web-view>
		</view>
		<rudon-payWithPassword v-if="data.isShow && form.take==0 " :displayByParent="pluginShowing" :pwdLength="6"
			:isSinger="goodslist[0]&&goodslist[0][0].is_single" :zkfc="Number(zkfc)" :price="Number(totalprice) + Number(freight)"
			:amount="$toFiexd(Number(totalprice) + Number(freight) +Number(zkfc),2)" titleWindow="请输入支付密码"
			titleAmount="支付购买商品" desc=" " descColor="#FB7B00" :needLog="true" :plaintext="false"
			@whenFullfilled="whenPwdReady">
		</rudon-payWithPassword>
		<rudon-payWithPassword v-if="data.isShow && form.take==1" :displayByParent="pluginShowing" :pwdLength="6"
			:isSinger="goodslist[0]&&goodslist[0][0].is_single" :zkfc="Number(zkfc)" :price="Number(totalprice) "
			:amount="$toFiexd(Number(totalprice) +Number(zkfc),2)" titleWindow="请输入支付密码" titleAmount="支付购买商品" desc=" "
			descColor="#FB7B00" :needLog="true" :plaintext="false" @whenFullfilled="whenPwdReady">
		</rudon-payWithPassword>
	</view>
</template>
<script setup>
	import {
		ref,
		getCurrentInstance,
		reactive
	} from "vue";
	import {
		onLoad,
		onShow,
		onUnload
	} from "@dcloudio/uni-app";
  import {is_weixin} from "@/utils/isWeixinH5";

	const {
		proxy
	} = getCurrentInstance();
  // 0, '消费券'), (1, '微信'), (2, '支付宝'),  (3 '银行卡'), 4 补贴券)
	let paylist = ref(
      [
      {
        "id": "0",
        "name": "消费券",
        "show": "1",
        "sort": "0",
        "url": "https://pili-vod.guanxikeji.com/fubao/img/1755654395283.png"
      },
      {
        "id": "1",
        "name": "微信",
        "show": "1",
        "sort": "0",
        url: "https://pili-vod.guanxikeji.com/fubao/img/1757311647971.png"
      },
      {
        "id": "2",
        "name": "支付宝",
        "show": "1",
        "sort": "77",
        url: "https://pili-vod.guanxikeji.com/fubao/img/1757311629380.png"
      },
      // {
      //   "id": "4",
      //   "name": "银行卡",
      //   "show": "1",
      //   "sort": "0",
      //   "url": "https://img.fubaozx.cn/static/oldImg/16995010958070.png"
      // },
      {
        "id": "5",
        "name": "补贴券",
        "show": "1",
        "sort": "0",
        "url": "https://pili-vod.guanxikeji.com/fubao/img/1755654411897.png"
      }
  ]
  ) //支付方式
  if(is_weixin()){
    paylist.value=paylist.value.filter(item => item.id!= 2)

  }
	let current = ref(0)
	let address = ref({})
	let id = ref('')

	const money = ref('') //金额
	const key = ref(true) //数字键盘
	const notes = ref('') //备注
	const orderID = ref(true) //订单编号
	const butdsb = ref(false) //支付按钮禁用
	const code = ref('') //获取code
	const type = ref(2) //0微信 1支付宝 2微信公众号
	const newCode = ref(1) //0微信 1公众号
	const is_h5 = ref(3) //0 app 1 h5 2 微信小程序 3 微信公众号
	const open_id = ref('')
	let form = ref({
		take: 0,
		address: '',
		remarks: '',
		pay_type: 0,
		password: '',
		order: '',
		bank_id: 0,
		sms_code: '',
		mch_order_no: '',
		is_h5: 0,
		logo: '',
		name: '',
		title: '',
	})
	let data = reactive({
		is_cash: 0,
		html: '',
		bakn: {},
		time: '获取验证码',
		list: [],
		bankstate: 0,
		logo: '',
		name: '',
		title: '',
		merchant: '',
		order: '',
		money: '',
		isShow: false, //是否显示支付
	})
	let goodslist = ref({}) //商品
	let totalprice = ref(0)
	let allpric = ref(0) //所有的钱
	let zkfc = ref(0) //zkfc
	let freight = ref(0)
	let pluginShowing = ref(false)
	let is_pay_password = ref(false)
	let share = ref()
	let networkRef = ref()
	let networkDetail = ref()
	let alipayform = ref()
	let network = ref([])
	let paymentData = ref({})
	let wxPayData = ref({})
	let settime = ref(null)
	let isFlag = ref(0) // 支付定时判断
	let userInfo = ref('')
	onLoad((option) => {
		console.log('页面onLoad', option)
		id.value = option.id
		form.value.order = option.id
		// paylist.value = uni.getStorageSync('payType')
		// console.log(paylist.value, 11111111111111111111111111111)
		// paylist.value[2].show = 1
		getAddress()
		userInfo.value = uni.getStorageSync('userInfo')
	})
  //返回事件
  onBackPress(()=>{
    uni.removeStorageSync('payOrder');
  })
	onShow(() => {
    const payOrder=uni.getStorageSync('payOrder');

		isFlag.value = uni.getStorageSync('ordertype') || 0
		uni.setStorageSync('ordertype', 0)
		if (isFlag.value == 1||payOrder) {
			clearInterval(settime.value)
      setTimeout(()=>{
        getpayresylt(5,payOrder)

      },1500)
		}
		// clearInterval(settime.value)
		//重新获取支付方式
		proxy.$payList()
		// #ifdef H5
		// paylist.value[2].show = 1
		// #endif
		// #ifdef APP-PLUS
		form.value.is_h5 = 0
		// #endif
		// #ifdef H5
		if (proxy.$isWeiXinBrowser()) {
			form.value.is_h5 = 3
		} else {
			form.value.is_h5 = 1
		}
		// #endif
		// #ifdef MP-WEIXIN
		form.value.is_h5 = 2
		// #endif
		proxy.$request({
			url: `api/pay_bank/?page=1&size=50`,
			method: 'get',
			success(res) {
				data.list = res.data.data
				if (res.data.data.length > 0) {
					data.bakn = res.data.data[0]
					form.value.bank_id = res.data.data[0].ID
				}
			}
		})
		proxy.$request({
			url: `api/network/`,
			method: 'get',
			success(res) {
				network.value = res.data
			}
		})

		is_pay_password.value = uni.getStorageSync('userInfo').is_pay_password ? true : false
		uni.$on('selectAddress', (info) => { //开启监听全局的地址改变事件
			console.log(info, "info")
			address.value = info
			init(id.value, info.id)
			uni.$off('selectAddress');
		})
	})

	onUnload(() => {
		uni.$off('selectAddress') //在此生命周期里销毁地址改变事件的监听
		clearInterval(settime.value)
	})

	function delOrder(ids, key, index) {
		proxy.$request({
			url: `api/delOrder/`,
			method: 'POST',
			data: {
				id: ids,
			},
			success: (res) => {
				uni.showToast({
					icon: 'none',
					title: res.errmsg
				})
				if (res.errno == 0) {
					let list = JSON.parse(JSON.stringify(goodslist.value))
					console.log(list)
					let num = 0
					for (let var1 in list) {
						list[var1].forEach(item => {
							num++
						})
					}
					if (num == 1) {
						uni.navigateBack()
					} else {
						list[key].splice(index, 1)
					}
					init(id.value, address.value['id'])
				}

			}
		})
	}
	// 打开银行卡列表
	function binding() {
		share.value.open('bottom')
	}
	// 选取银行卡
	function selectBank(item) {
		data.bakn = item
		form.value.bank_id = item.ID

		share.value.close()
	}

	function pay(i) {
		//先判断是否能发货
		let is_deliver = false
		for (let i in goodslist.value) {
			goodslist.value[i].forEach((it) => {
				if (it.is_deliver == 1) {
					is_deliver = true
				}
			})
		}

		if (is_deliver) {
			uni.showToast({
				icon: 'none',
				title: '部分商品在该地址下不能发货！'
			})
		} else {
			isFlag.value = 1
			uni.setStorageSync('ordertype', isFlag.value);
			data.bankstate = i
			if (JSON.stringify(address.value) == '{}') {
				uni.showToast({
					icon: 'none',
					title: '请选择收货地址'
				})
				return
			}
			console.log(address.value)
			form.value.address = address.value.id
			if (form.value.pay_type == 0||form.value.pay_type == 5) {
				if (!is_pay_password.value) {
					uni.showModal({
						title: '您还没有设置支付密码',
						content: '去设置支付密码?',
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击确定');
								proxy.$goURl('/pages/personal/password/password?code=1')
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
					return
				}

				pluginShowing.value = true
				setTimeout(() => {
					pluginShowing.value = false
				}, 200)
			}
			if (form.value.pay_type == 1) {
        rechargePay(2)
			}
			if (form.value.pay_type == 2) {
        rechargePay(1)

			}
			if (form.value.pay_type == 3) {
				whenPwdReady('')
			}
			if (form.value.pay_type == 4) {
				whenPwdReady('')
			}
		}

	}

  const rechargePay = (type) => {
    const queryData={
      ...form.value,
      type,
      pay:is_weixin()?0:1

    }
    console.log(queryData)

    proxy.$request({
      url: `api/payment/`,
      method: "POST",
      data: queryData,
      success(res) {
        if (res.errno == 0) {
          console.log(res.data)
          // orderRef.value = res.data.order
          uni.setStorageSync('payOrder', res.data.order);
          // #ifdef H5
          window.location.href = res.data.url
          // #endif
          // #ifdef APP-PLUS
          plus.runtime.openURL(res.data.url);
          // #endif
        }

        if (res.errmsg) {
          uni.showToast({
            title: res.errmsg,
            icon: "none"
          });
        }

      }
    });
  };

	function base64_encode(str) {
		var c1, c2, c3;
		var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
		var i = 0,
			len = str.length,
			string = '';

		while (i < len) {
			c1 = str.charCodeAt(i++) & 0xff;
			if (i == len) {
				string += base64EncodeChars.charAt(c1 >> 2);
				string += base64EncodeChars.charAt((c1 & 0x3) << 4);
				string += "==";
				break;
			}
			c2 = str.charCodeAt(i++);
			if (i == len) {
				string += base64EncodeChars.charAt(c1 >> 2);
				string += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
				string += base64EncodeChars.charAt((c2 & 0xF) << 2);
				string += "=";
				break;
			}
			c3 = str.charCodeAt(i++);
			string += base64EncodeChars.charAt(c1 >> 2);
			string += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));
			string += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));
			string += base64EncodeChars.charAt(c3 & 0x3F);
		}
		return string;
	}
	// 调起微信支付
	function gowxpay() {
		// 公众号
		if (appContext.config.globalProperties.$isWeiXinBrowser()) {
			if (!open_id.value) {
				uni.showToast({
					icon: 'none',
					title: 'open_id过期,刷新中',
					duration: 1500,
				})
				wxLogin()
				return
			}
			if (!code.value) {
				uni.showToast({
					icon: 'none',
					title: 'code过期,刷新中',
					duration: 1500,
				})
				wxLogin() //调用获取code
				return
			}
		}


		uni.$request({
			url: 'https://tool.guanxikeji.com/admin/wxPay/',
			method: 'POST',
			data: {
				open_id: open_id.value,
				id: parseFloat(id.value),
				money: parseFloat(data.money),
				notes: notes.value,
				order: order,
				project: project,
				is_h5: is_h5.value,
				code: newCode.value,
			},
			success: (res) => {
				console.log('提交信息回调成功', res);
				var data = res.data
				if (res.errno == 0) {
					if (appContext.config.globalProperties.$isWeiXinBrowser()) {
						// 公众号↓↓↓
						try {
							console.log('WeixinJSBridge', WeixinJSBridge)
							WeixinJSBridge.invoke('getBrandWCPayRequest', {
								"appId": data.appid, //公众号名称，由商户传入
								"timeStamp": data.timeStamp, //时间戳，自1970年以来的秒数
								"nonceStr": data.nonceStr, //随机串
								"package": data.package,
								"signType": data.signType, //微信签名方式：
								"paySign": data.paySign //微信签名
							}, function(res) {
								if (res.err_msg == "get_brand_wcpay_request:ok") {
									//支付成功
									uni.showToast({
										icon: 'none',
										title: '支付成功',
										duration: 1500,
									})

								} else if (res.err_msg == "get_brand_wcpay_request:cancel") {
									//支付取消
									uni.showToast({
										icon: 'none',
										title: '支付取消',
										duration: 1500,
									})
									postFail()
									butdsb.value = false //启用支付按钮
								} else if (res.err_msg == "get_brand_wcpay_request:fail") {
									//支付失败
									uni.showToast({
										icon: 'none',
										title: '支付失败',
										duration: 1500,
									})
									postFail()
									butdsb.value = false //启用支付按钮
								} //res.err_msg将在用户支付成功后返回ok
							});
						} catch (e) {
							uni.showToast({
								icon: 'none',
								title: '支付失败!',
								duration: 1500,
							})
							postFail()
							console.log("错误捕获", e)
							butdsb.value = false //启用支付按钮
						}
					}
				}
				if (res.errno == 1) {
					uni.showToast({
						icon: 'none',
						title: res.errmsg,
						duration: 1500,
					})
					butdsb.value = false //启用支付按钮
				}
				if (res.errno == 2) {
					uni.showToast({
						icon: 'none',
						title: 'code有误,重新获取中',
						duration: 1500,
					})
					getinfo()
					butdsb.value = false //启用支付按钮
					return
				}
			},
			fail: (err) => {
				console.log('failed', err);
				uni.showToast({
					icon: 'none',
					title: '支付失败',
					duration: 1500,
				})
				butdsb.value = false //启用支付按钮
			}
		});

	}

	// 获取url参数code方法
	function getQueryString() {
		uni.login({
			provider: 'weixin',
			success: function(loginRes) {

				return loginRes.code
			}
		});
	}
	// 传给后端的取消支付
	function postFail() {
		appContext.config.globalProperties.$request({
			url: `/admin/cancellationPay/`,
			method: 'POST',
			data: {
				id: id.value,
			},
			success: (res) => {
				// uni.showToast({
				// 	icon: 'none',
				// 	title: '支付取消',
				// 	duration: 1500,
				// })
			},

		})
	}
	// 调起公众号验证
	function wxLogin() {


		const appid = 'wxb84332960546c10c';
		const url = 'https://www.fubaozx.cn/h5/#/pages/order/createorder/';
		const h5 = url + `?id=${data.merchant}&order=${data.order}&money=${data.money}`;
		// const h5 = `http://192.168.2.6:5173/scan/?id=${id.value}`;
		let newurl =
			`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(h5)}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`;
		console.log("跳转url:", url)
		console.log("跳转目标地址:", h5)
		window.location.href = newurl;
	}
	//查看是否支付成功
	function getpayresylt(num,order=null) {
		console.log(num, "numnum")
		if (num > 10) {
			clearInterval(settime.value)
		} else {
			proxy.$request({
				url: `api/paymentQuery/?order=${order ||id.value}`,
				method: 'get',
				success: (res) => {
					// console.log(res, "查看结果",order,order,)
          // uni.showToast({
          //   icon: 'none',
          //   title: (res.data+'---'+res.errno),
          // })
					if (res.errno == 0) {
						clearInterval(settime.value)
						setTimeout(() => {
							uni.setStorageSync('ordertype', 0)
              uni.removeStorageSync('payOrder');

							proxy.$goURl('/pages/order/paymentresult')
						}, 1000)
					}
				}
			})
		}

	}

	function init(id, address_id) {
		proxy.$request({
			url: `api/order/`,
			method: 'PUT',
			data: {
				order: id,
				address_id: address_id
			},
			success: (res) => {
        if(res.errno!= 0) {
          uni.showToast({
            icon: 'none',
            title: res.errmsg
          })
          return
        }
        const list= Object.values(res.data)
        console.log('res',res.data,res.data[0])
				// if (res.data[0][0].is_single == 2) {
				// 	paylist.value.forEach((it) => {
				// 		if (it.id == "0") {
				// 			it.name = ''
				// 			it.name = 'DD支付'
				// 		}
				// 		return it
				// 	})
				// }
				// res.data.forEach(item => {
				// 	totalprice.value += item.price * item.number
				// })
				totalprice.value = 0
				zkfc.value = 0
				freight.value = 0
				let moneys=0
				for (let var1 in res.data) {
					// moneys+=res.data[var1][0].freight
					freight.value += res.data[var1][0].freight
					res.data[var1].forEach(item => {
						totalprice.value += item.price * item.number
						zkfc.value += item.coupon * item.number
						allpric.value = totalprice.value + '+zkfc:' + zkfc.value
					})
				}
				data.isShow = true
				// totalprice.value+=moneys
				goodslist.value = list
				// console.log("order", goodslist.value[0][0])
			}
		})
	}

	function getAddress() {
		proxy.$request({
			url: `api/address/?page=1&size=20&is_default=1`,
			method: 'get',
			success: (res) => {
				console.log(res)
				if (res.data.ID) {
					address.value = res.data
					address.value['id'] = res.data.ID
					form.value.address = res.data.ID
					init(id.value, res.data.ID)

				} else {
					address.value = {}
					init(id.value, 0)
				}
			}
		})
	}

	function radioChange(evt) {
		console.log('evt.detail.value', evt.detail.value)
		form.value.pay_type = Number(evt.detail.value)
	}

	function toWxMpFn(data) {
		// console.log("toWxMpFn", e)
		let _type = null
		let xx = encodeURI('merchant=1')
		let _wechatTurnUrl = `pages/index/index?merchant=${data.id}&order=${data.order}&money=${data.money}`
		let _id = 'gh_6b7694461b14'
		try {
			//需调用plus.share.getServices获取微信分享服务对象
			plus.share.getServices((s) => {
				let sweixin = {};
				for (let i = 0; i < s.length; i++) {
					let share = s[i];
					if (share.id === 'weixin') {
						sweixin = share
					}
				}
				//小程序参数，必填
				let WeixinMiniProgramOptions = {
					id: _id,
					path: _wechatTurnUrl, //可指定打开的页面
					type: 2
				}
				sweixin ? sweixin.launchMiniProgram(WeixinMiniProgramOptions) : plus.nativeUI.alert(
					'当前环境不支持微信操作!');
			}, function(e) {
				console.log("获取分享服务列表失败：" + e.message);
			});
		} catch (err) {

			console.log('wmFn', err)
		}
	}
	// 密码支付 - 当密码填满时回调的函数
	function whenPwdReady(e) {
		if (form.value.pay_type != 4) {
			form.value.bank_id = 0
		}
		if (data.bankstate == 0) {
			form.value.sms_code = ''
			form.value.mch_order_no = ''
		}
		if (data.bankstate == 1 && form.value.pay_type == 4) {
			if (!form.value.bank_id) {
				uni.showToast({
					icon: 'none',
					title: '请选择银行卡'
				})
				return
			}
			if (!form.value.sms_code) {
				uni.showToast({
					icon: 'none',
					title: '请输入验证码'
				})
				return
			}
		}
		form.value.password = e
		proxy.$request({
			url: `api/payment/`,
			method: 'post',
			data: form.value,
			success: (res) => {
				console.log('resres',form.value.pay_type, res.errno == 0)
				if (form.value.pay_type == 0||form.value.pay_type == 5) {
					pluginShowing.value = false
					if (res.errno == 0) {
						uni.showToast({
							icon: 'none',
							title: res.errmsg
						})
						setTimeout(() => {
							proxy.$goURl('/pages/order/paymentresult')
						}, 1000)

					} else {
						uni.showToast({
							icon: 'none',
							title: res.errmsg
						})
					}
				} else if (form.value.pay_type == 2) {
					if (res.errno == 0) {
						// #ifdef APP-PLUS
						uni.requestPayment({
							provider: 'alipay',
							orderInfo: res.data, //支付宝订单数据
							success: function(resss) {
								uni.showToast({
									icon: 'none',
									title: '支付成功'
								})
								setTimeout(() => {
									proxy.$goURl('/pages/order/paymentresult')
								}, 1000)
							},
							fail: function(err) {
								uni.showToast({
									icon: 'none',
									title: '支付失败'
								})
							}
						});
						// #endif
						// #ifdef H5
						window.location.href = "https://openapi.alipay.com/gateway.do?" + res.data
						console.log("https://openapi.alipay.com/gateway.do?" + res.data)
						// #endif
					} else {
						uni.showToast({
							icon: 'none',
							title: res.errmsg
						})
					}
				} else if (form.value.pay_type == 3) {

					if (res.errno == 0) {
						// console.log('res.data.rb_CodeMsg',res.data.rb_CodeMsg)
						if (res.data.rc_Result) {
							let indexs = 0
							let listsss = res.data.rc_Result.split("'")
							listsss.forEach((item, index) => {
								if (item == '>location.href=') {
									indexs = index + 1
									console.log(indexs)
								}
							})
							try {
								window.location.href = listsss[indexs]
							} catch (e) {
								data.html = listsss[indexs]
							}

							setTimeout(() => {
								data.html = ""
								uni.switchTab({
									url: '/pages/personal/index'
								})
							}, 2000)
						} else {
							uni.showToast({
								icon: 'none',
								title: res.data.rb_CodeMsg
							})
						}
					} else {
						uni.showToast({
							icon: 'none',
							title: res.errmsg
						})
					}

					// uni.navigateTo({
					// 	url:'/pages/applet/webview?html='+res.data.rc_Result
					// })
				} else {
					uni.showToast({
						icon: 'none',
						title: res.errmsg
					})
					if (res.errno == 0 && data.bankstate == 0) {
						form.value.mch_order_no = res.data.mch_order_no
						let times = 60
						let timer = setInterval(function() {
							if (times == 0) {
								data.time = '获取验证码'
								clearInterval(timer)
							} else {
								times--
								data.time = times + 'S重发'
							}
						}, 1000)
					} else if (res.errno == 0) {
						uni.showLoading({
							title: '支付中'
						});
						let i = 0
						let timer = setInterval(() => {
							i++
							proxy.$request({
								url: `api/paymentQuery/?order=${id.value}`,
								method: 'get',
								success: (resData) => {
									if (i == 3 || resData.errno == 0 || resData.errno ==
										1) {
										uni.hideLoading();
										uni.showToast({
											icon: 'none',
											title: resData.errmsg
										})
										if (resData.errno == 0) {
											setTimeout(() => {
												proxy.$goURl(
													'/pages/order/paymentresult'
												)
											}, 1000)
										} else {
											setTimeout(() => {
												uni.navigateBack()
											}, 1000)

										}
										clearInterval(timer)
									}
								}
							})

						}, 2000)

					} else {


					}
				}

			}
		})
	}

	function paymentQuery() {

	}
</script>


<style lang="scss" scoped>
page{
	//background-color: #fff;
}
	.container {
		.take {
			display: flex;
			background-color: #fff;
			border-bottom: 20upx solid #f5f5f5;

			.item {
				width: 50%;
				text-align: center;
				padding: 20upx;
			}

			.active {
				color: $color4;
			}
		}

		.header {
			background-color: #fff;
			padding: 30upx 30rpx;
			//margin: 0 30rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 1rpx solid #DDDDDD;

			.left {
				.address {
					font-size: 28upx;
					color: #000;
					font-weight: 500;
				}

				.addressxx {
					font-weight: 400;
					font-size: 24rpx;
					color: #2D2D2D;
					margin-top: 10rpx;
				}

				.info {
					font-size: 24upx;
					color: #333333;
				}
			}
		}

		.goodsbox {
      &.goodsbox2{
        margin: 20rpx;

      }

			border-radius: 30upx;
			background-color: #fff;

			.business {
				display: flex;
				align-items: center;
				font-size: 32upx;
				font-weight: bold;
				padding: 0 20upx 20upx 20upx;
				border-bottom: 1upx solid #f5f5f5;
				margin-bottom: 30upx;

				image {
					width: 60upx;
					height: 60upx;
					border-radius: 50%;
					margin-right: 10upx;
				}
			}

			.goods {
				display: flex;
				margin-bottom: 30upx;
				position: relative;
				border-bottom: 1rpx solid #DDDDDD;
				.delOrder {
					position: absolute;
					right: 5upx;
					top: 170upx;
				}

				image {
					width: 142rpx;
					height: 142rpx;
					border-radius: 16rpx;
					margin-right: 24rpx;
				}

				.right {
					width: calc(100% - 180upx);

					.name{
						font-weight: 400;
						font-size: 28rpx;
						color: #2D2D2D;
					}

					.gg {
						color: #999999;
						font-size: 24upx;
						margin: 20upx 0;
					}

					.num {
						color: #999999;
						font-size: 24upx;
						display: flex;
						justify-content: space-between;
						align-items: center;
					}
				}
			}

			.titlebox {
				display: flex;
				justify-content: space-between;
				align-items: center;
				color: #333333;
				height: 96rpx;
				.titleboxl {
					font-size: 28upx;
					font-weight: 500;
				}

				.titleboxr {
					font-weight: 500;
					font-size: 28rpx;
					color: #2D2D2D;
				}



				.uni-input {
					font-size: 28upx;
					width: calc(100% - 150upx);
				}
			}

			.paylist {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin: 20upx 0;

				.left {
					display: flex;
					align-items: center;

					image {
						width: 40upx;
						height: 40upx;
						margin-right: 20upx;
					}
				}

			}
		}

		.footer {
			position: fixed;
			background-color: #fff;
			bottom: 0;
			left: 0;
			right: 0;

			.time {
				color: #DE5B2A;
			}

			.foot {

				display: flex;
				justify-content: space-between;
				align-items: center;
				font-size: 32upx;
				height: 136rpx;
				background: #FFFFFF;
				box-shadow: 0px -8rpx 20rpx 0px #F6F6F6;
				padding-right:30rpx ;
				.left {
					display: flex;
					padding: 0 30rpx;
					font-weight: 400;
					font-size: 26rpx;
					color: #2D2D2D;
					align-items: center;
					.price {
						font-weight: 500;

						color: #976029;
						display: flex;
						align-items: center;
						text{
							font-size: 30rpx;
						}
					}
				}

				.right {
					width: 344rpx;
					height: 80rpx;
					background: #F63B29;
					border-radius: 16rpx;
					font-weight: 500;
					font-size: 32rpx;
					color: #FFFFFF;
					text-align: center;
					line-height: 80rpx;
				}
			}
		}

	}

	.popupcontent {
		background-color: #f5f5f5;
		border-radius: 30upx 30upx 0 0;
		overflow: hidden;

		.btn {
			background-color: $color1;
			margin: 20upx;
			padding: 20upx;
			text-align: center;
			color: #fff;
			border-radius: 40upx;
		}
	}

	.backList {
		display: flex;
		align-items: center;
		background-color: #fff;
		justify-content: space-between;
		padding: 20upx 0;
		border-radius: 20upx;
		margin: 30upx;

		.backImg {
			width: 48rpx;
			height: 48rpx;
		}

		.backNames {
			margin-left: 6px;
			display: flex;
			align-items: center;

			.backName,
			.backId {
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC-Regular;
				font-weight: 400;
				text-align: left;
				color: #333333;
				margin: 0 10px;
			}

			.backName {
				margin-left: 10px;
			}
		}

		.backNamesm {
			margin: 0 6px;

			text {
				font-size: 16px;
				color: #333333;
			}
		}

		.selects {
			width: 14px;
			height: 15px;
		}
	}

	.sharep {
		width: 100%;
		margin: 30upx;

		.selectBank {
			width: 94%;
			margin: 0 auto;
			position: relative;
			text-align: center;
			padding: 15px 0;
			border-top-left-radius: 20px;
			border-top-right-radius: 20px;

			.selectMdd {
				color: #333333;
				font-size: 30upx;
				font-weight: 600;
			}

			.popupIcon {
				position: absolute;
				right: 0%;
				top: 10px;
			}
		}

		.information {
			padding: 10px;
			height: 300px;
			background-color: #f5f5f5;
			overflow-y: scroll;

			.BankList {
				background-color: #fff;
				padding: 15px;
				border-radius: 10upx;
				margin-bottom: 30upx;

				.nameAndBankName {
					margin-bottom: 4px;
					color: #333;

					.name {
						margin-right: 10px;
					}
				}

				.bankAddress {
					color: #333;

					.address {
						margin-right: 10px;
					}
				}
			}
		}
	}

	.itembox {
		display: flex;
		align-items: center;
		justify-content: space-between;
		background-color: #fff;
		padding: 20upx;
		border-radius: 20upx;
		font-size: 14px;
		margin: 30upx;

		.label {
			width: 30%;
		}

		.input {
			width: 70%;
			justify-content: space-between;
		}
	}
  .box_cnt{
    padding: 20rpx;
    box-sizing: border-box;
  }
</style>
